package cn.fl.insurance.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.insurance.dto.InsFeeDeductRecDTO;
import cn.fl.insurance.condition.InsFeeDeductRecQueryCondition;
import cn.fl.insurance.constant.InsFeeDeductRecConstant;
import cn.fl.insurance.api.IInsFeeDeductRecApiService;
import cn.fl.insurance.service.IInsFeeDeductRecService;
import cn.fl.insurance.service.IInsPlcBscInfoApiService;
import cn.fl.insurance.transform.InsFeeDeductRecVOTransform;
import cn.fl.insurance.vo.InsFeeDeductRecVO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.List;
import java.util.Map;

/**
 * @Description: 保险违约金扣除记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsFeeDeductRecServiceImpl implements IInsFeeDeductRecService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsFeeDeductRecServiceImpl.class);
	
	/**
	 * 保险违约金扣除记录表 暴露服务
	 */
	@Autowired
	private IInsFeeDeductRecApiService insFeeDeductRecApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    
    @Autowired
    private IInsPlcBscInfoApiService insPlcBscInfoAipService;

	@Override
	public InsFeeDeductRecVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		InsFeeDeductRecDTO dto = this.insFeeDeductRecApiService.selectInsFeeDeductRecById(id);
		return InsFeeDeductRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(InsFeeDeductRecVO entity) throws BaseException{
		if(null == entity) return null;
		return this.insFeeDeductRecApiService.addInsFeeDeductRec(InsFeeDeductRecVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.insFeeDeductRecApiService.removeInsFeeDeductRecByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(InsFeeDeductRecVO entity) throws BaseException{
		if(null == entity) return false;
		return this.insFeeDeductRecApiService.modifyInsFeeDeductRec(InsFeeDeductRecVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        InsFeeDeductRecQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new InsFeeDeductRecQueryCondition());
        }
		condition=(InsFeeDeductRecQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.insFeeDeductRecApiService.searchInsFeeDeductRecListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<InsFeeDeductRecVO> voList=InsFeeDeductRecVOTransform.toVOList((List<InsFeeDeductRecDTO>)page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<InsFeeDeductRecVO> searchList(InsFeeDeductRecQueryCondition condition) throws BaseException{
		if(null == condition) condition=new InsFeeDeductRecQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<InsFeeDeductRecVO> voList=InsFeeDeductRecVOTransform.toVOList(this.insFeeDeductRecApiService.searchInsFeeDeductRecList(condition));
		this.setDictData(voList);
		return voList;
	}
	
	/**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<InsFeeDeductRecVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		//费用类型
		Map<String, String> feeTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), InsFeeDeductRecConstant.CODE_FEE_TYP_CD);
		//保证金处理方式
		Map<String, String> handleWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), InsFeeDeductRecConstant.CODE_HANDLE_WAY_CD);
		//状态
		Map<String, String> deductStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), InsFeeDeductRecConstant.CODE_DEDUCT_STS_CD);
		//保单性质
		Map<String, String> plcPropCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), InsFeeDeductRecConstant.CODE_PLC_PROP_CD);
		
		for(InsFeeDeductRecVO vo:voList){
			//费用类型(名字)
			if (feeTypCdMap != null && feeTypCdMap.containsKey(vo.getFeeTypCd())) {
            	vo.setFeeTypNm(feeTypCdMap.get(vo.getFeeTypCd()));
        	}
			//保证金处理方式
			if(handleWayCdMap != null && handleWayCdMap.containsKey(vo.getHandleWayCd())){
				vo.setHandleWayNm(handleWayCdMap.get(vo.getHandleWayCd()));
			}
			//状态
			if(deductStsCdMap != null && deductStsCdMap.containsKey(vo.getDeductStsCd())){
				vo.setDeductStsNm(deductStsCdMap.get(vo.getDeductStsCd()));
			}
			//保单性质
			if(plcPropCdMap != null && plcPropCdMap.containsKey(vo.getPlcPropCd())){
				vo.setPlcPropNm(plcPropCdMap.get(vo.getPlcPropCd()));
			}
		}
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }
    
    /**
     * 发起转正常申请
     *
     * @param condition
     * @return
     * @throws BaseException
     */
	@Override
	public DataResultVO addRenewApply(InsFeeDeductRecQueryCondition insFeeDeductRecQueryQC) throws BaseException {
		DataResultVO resultVO= null;
		insFeeDeductRecQueryQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
		insFeeDeductRecQueryQC.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		insFeeDeductRecQueryQC.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		insFeeDeductRecQueryQC.setUsrId(CurrentThreadContext.getCurrentUserId());
		insFeeDeductRecQueryQC.setUsrNm(CurrentThreadContext.getCurrentUserName());
        //发起续保流程
        DataResultDTO resultDTO=this.insFeeDeductRecApiService.addRenewApply(insFeeDeductRecQueryQC);
        if(resultDTO!=null){
            resultVO=new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo());
        }else{
            resultVO=new DataResultVO(BaseConstant.IS_YESNO_NO,"流程发起失败");
        }
        return resultVO;
	}

}
